<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="okhttp3.OkHttpClient" %>
<%@ page import="okhttp3.Request" %>
<%@ page import="okhttp3.Response" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="java.util.concurrent.TimeUnit" %>
<%
    /**
     * 秘钥组成： 自定义用户名+“.”+当前时间戳(到秒)+“.”+(当前时间戳（到秒）+自定义密码)的MD5码
     * 如： admin.1503930878.e67656eb651abfa4c3cfd2a8cbbe9cf1
     */
    BaseBean baseBean = new BaseBean();
    JSONObject jsonData = new JSONObject();
    User user = HrmUserVarify.getUser(request, response);
    String loginid = user.getLoginid();
    baseBean.writeLog(">>>>>pdmSSO.jsp>>>>loginid=" + loginid);
    String apiUrl = "http://192.168.1.20:11250/foundation-cloud-service/foundation-cloud-service/loginControllerSSO/loginNoPwd?userName=" + loginid;
    baseBean.writeLog(">>>>>pdmSSO.jsp>>>>apiUrl=" + apiUrl);
    JSONObject resultJson = sendOKHttpGetRequest(apiUrl);
    baseBean.writeLog(">>>>>pdmSSO.jsp>>>>resultJson=" + resultJson);
    String message = Util.null2String(resultJson.getString("message"));
    String token = "";
    if ("success".equals(message)) {
        JSONObject resultDataJson = resultJson.getJSONObject("data");
        String code = Util.null2String(resultDataJson.getString("code"));
        if ("200".equals(code)) {
            JSONObject resultJsonObject =resultDataJson.getJSONObject("result");
             token = Util.null2String(resultJsonObject.getString("token"));
        }
    }
    jsonData.put("token", token);
    out.print(jsonData.toJSONString());
%>
<%!


    /**
     * get请求
     *
     * @param apiUrl
     * @return
     */
    private JSONObject sendOKHttpGetRequest(String apiUrl) {
        JSONObject resultJson = new JSONObject();
        String message = "";
        String data = "";
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .build();
            Request request = new Request.Builder()
                    .url(apiUrl)
                    .get()
                    .build();
            Response response = client.newCall(request).execute();
            String result = Util.null2String(response.body().string());
            if (!"".equals(result)) {
                data = result;
                message = "success";
            } else {
                message = "response result is null";
            }
        } catch (Exception e) {
            message = "response timeout";
            e.printStackTrace();
        }
        resultJson.put("data", data);
        resultJson.put("message", message);
        return resultJson;
    }

%>